package com.nebula.dao.impl;

import com.nebula.entity.Navigation;
import com.nebula.entity.RoleNavigation;
import com.nebula.dao.Database;
import com.nebula.dao.INavigationDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * @author CcccX
 * @create 2019-01-16 13:57
 */
public class NavigationDaoImpl extends BaseDaoImpl<Navigation> implements INavigationDao {

    @Override
    public void insertRoleNavigation(List<RoleNavigation> list) {
        Connection conn = Database.getConn();
        String sql = "insert into role_navigation(id,nav_id,role_id,created_time,updated_time) values (?,?,?,now()," +
                "now())";
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            for (RoleNavigation roleNavigation : list) {
                pstmt.setInt(1, roleNavigation.getId());
                pstmt.setString(2, roleNavigation.getNavId());
                pstmt.setInt(3, roleNavigation.getRoleId());
                pstmt.addBatch();
                System.out.println(pstmt);
            }
            pstmt.executeBatch();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int deleteRoleNavigationByNavId(String navId) {
        Connection conn = Database.getConn();
        String sql = "delete from role_navigation where nav_id = ?";
        int resultCount = 0;
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, navId);
            resultCount = pstmt.executeUpdate();
            System.out.println(pstmt);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return resultCount;
    }
}
